//
//  RHLocalDevTopoCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/3/26.
//

#import "RHLocalDevTopoCell.h"
#import "YYAnimatedImageView.h"
#import "YYImage.h"

@interface RHLocalDevTopoCell()
@property (nonatomic, strong) UIImageView *topoImg;

@property (nonatomic ,strong) UIView *feed_indicator;

@property (nonatomic ,strong) UIView *battery_indicator;

@property (nonatomic ,strong) UIView *power_indicator;

@property (nonatomic ,strong) UIView *load_indicator;

@property (nonatomic ,strong) UIView *solar_indicator;

@property (nonatomic, strong) YYAnimatedImageView *loadGif;

@property (nonatomic, strong) YYAnimatedImageView *solarGif;

@property (nonatomic, strong) YYAnimatedImageView *gridGif;

@property (nonatomic ,strong) QMUILabel *feedLab;

@property (nonatomic ,strong) QMUILabel *batteryLab;

@property (nonatomic ,strong) QMUILabel *powerLab;

@property (nonatomic ,strong) QMUILabel *loadLab;

@property (nonatomic ,strong) QMUILabel *solarLab;
//当日供电
@property (nonatomic ,strong) QMUILabel *dchgTodayLab;
@end
@implementation RHLocalDevTopoCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}
//@"Ems_PvPwr",@"Ems_GridPwr",@"Ems_LoadPwr",@"Ems_BatPwr",@"Ems_BatSoc
-(void)setTopoDataDic:(NSDictionary *)topoDataDic
{
    _topoDataDic = topoDataDic;
    _loadGif.image = nil;
    _solarGif.image = nil;
    _gridGif.image = nil;
    
    if(topoDataDic[@"Ems_PvPwr"]){
        NSString *pvpwrStr = [NSDecimalNumber decimalStringWithDouble:[topoDataDic[@"Ems_PvPwr"] doubleValue]];
        self.solarLab.attributedText = [self attributeStringWithValue:pvpwrStr unit:@"kW"];
        
    }
    
    if(topoDataDic[@"Ems_GridPwr"]){
        NSString *gridStr = [NSDecimalNumber decimalStringWithDouble:[topoDataDic[@"Ems_GridPwr"] doubleValue]];
        self.feedLab.attributedText = [self attributeStringWithValue:gridStr unit:@"kW"];
       
    }
    
    if(topoDataDic[@"Ems_LoadPwr"]){
        NSString *loadStr = [NSDecimalNumber decimalStringWithDouble:[topoDataDic[@"Ems_LoadPwr"] doubleValue]];
        self.loadLab.attributedText = [self attributeStringWithValue:loadStr unit:@"kW"];
    }
    
    if(topoDataDic[@"Ems_BatPwr"]){//除以1000
        NSDecimalNumber *batValue = [NSDecimalNumber decimalNumberWithDouble:[topoDataDic[@"Ems_BatPwr"] doubleValue]];
        NSString *resultStr = [NSString stringWithFormat:@"%@",batValue];

        self.batteryLab.attributedText = [self attributeStringWithValue:resultStr unit:@"kW"];
    }
    
    //当日放电量
    if(topoDataDic[@"Ems_BDE_Day"]){
        NSString *socStr = [NSDecimalNumber decimalStringWithDouble:[topoDataDic[@"Ems_BDE_Day"] doubleValue]];
        self.dchgTodayLab.attributedText = [self attributeStringWithValue:socStr unit:@"kWh"];
    }
   
    //电网动画
    if ([topoDataDic[@"Ems_GridPwr"] floatValue]>0) {
        _gridGif.image = [YYImage imageNamed:@"topo_grid_battery.gif"];
    }
    if ([topoDataDic[@"Ems_GridPwr"] floatValue]<0) {
        _gridGif.image = [YYImage imageNamed:@"topo_battery_grid.gif"];
    }
        
    //光伏动画
    if([topoDataDic[@"Ems_PvPwr"] floatValue] > 0){
        _solarGif.image = [YYImage imageNamed:@"topo_solar_load.gif"];
    }
        
    //负载动画
    if([topoDataDic[@"Ems_LoadPwr"] floatValue]>0){
        _loadGif.image = [YYImage imageNamed:@"topo_battery_load.gif"];
    }

}

-(NSMutableAttributedString*)attributeStringWithValue:(NSString*)value unit:(NSString*)unitStr
{
    NSMutableAttributedString *mtbAtriStr = [[NSMutableAttributedString alloc]init];
    NSAttributedString *atrStr_value = [[NSAttributedString alloc]initWithString:value attributes:@{NSFontAttributeName:kSYSTEMFONT(14)}];
    NSAttributedString *atrStr_unit = [[NSAttributedString alloc]initWithString:unitStr attributes:@{NSFontAttributeName:kSYSTEMFONT(10)}];
    [mtbAtriStr appendAttributedString:atrStr_value];
    [mtbAtriStr appendAttributedString:atrStr_unit];
    return mtbAtriStr;
}

-(void)setupUI
{
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    kWeakSelf;
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.clearColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(0);
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.bottom.mas_offset(0);
    }];
    
    
    _topoImg = [[UIImageView alloc] init];
    [bgView addSubview:_topoImg];
    
    _loadGif = [[YYAnimatedImageView alloc]init];
    [bgView addSubview:_loadGif];
    [_loadGif mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.topoImg);
    }];
    
    _solarGif = [[YYAnimatedImageView alloc]init];
    [bgView addSubview:_solarGif];
    [_solarGif mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.topoImg);
    }];
    
    _gridGif = [[YYAnimatedImageView alloc]init];
    [bgView addSubview:_gridGif];
    [_gridGif mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.topoImg);
    }];
    
    //馈网
    _feed_indicator = [self indicatorViewWithTitle:@"电网".rh_localized unit:@"kW" Alignment:NSTextAlignmentCenter];
    _feedLab = [_feed_indicator viewWithTag:100];
    [bgView addSubview:_feed_indicator];
    
    
    //电池
    _battery_indicator = [self indicatorViewWithTitle:@"电池".rh_localized unit:@"kW" Alignment:NSTextAlignmentLeft];
    _batteryLab = [_battery_indicator viewWithTag:100];
    [bgView addSubview:_battery_indicator];
    
    
    //能量
    _power_indicator = [self indicatorViewWithTitle:@"能量".rh_localized unit:@"%" Alignment:NSTextAlignmentRight];
    _powerLab = [_power_indicator viewWithTag:100];
    [bgView addSubview:_power_indicator];
    
    
    //负载
    _load_indicator = [self indicatorViewWithTitle:@"负载".rh_localized unit:@"kW" Alignment:NSTextAlignmentLeft];
    _loadLab = [_load_indicator viewWithTag:100];
    [bgView addSubview:_load_indicator];
    
    
    //光伏
    _solar_indicator = [self indicatorViewWithTitle:@"光伏".rh_localized unit:@"kW" Alignment:NSTextAlignmentCenter];
    _solarLab = [_solar_indicator viewWithTag:100];
    [bgView addSubview:_solar_indicator];
    
    
    if([self.reuseIdentifier isEqualToString:@"RHLocalDevTopoCell_HE"]){
        self.productType = @"HE_BAT";
    }
    
    //能量(当日供电)
    UIView *energyView = kView;
    energyView.backgroundColor = UIColor.whiteColor;
    energyView.layer.cornerRadius = 8;
    [bgView addSubview:energyView];
    [energyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.mas_offset(kScaleW(80));
        make.top.equalTo(weakSelf.topoImg.mas_bottom).offset(kScaleW(65));
        make.bottom.mas_offset(-15);
    }];
    
    UIImageView *energyIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"local_energy"]];
    [energyView addSubview:energyIcon];
    [energyIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(40);
        make.left.mas_offset(15);
        make.centerY.mas_offset(0);
    }];
    
    QMUILabel *energyTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kRGB(63, 68, 67)];
    energyTitleLab.text = @"能量".rh_localized;
    [energyView addSubview:energyTitleLab];
    [energyTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(energyIcon.mas_right).offset(10);
        make.top.mas_offset(18);
    }];
    
    QMUILabel *contentLab1 = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kColorRGBA(63, 68, 67,0.6)];
    contentLab1.text = [NSString stringWithFormat:@"%@:",@"当日供电".rh_localized];
    [energyView addSubview:contentLab1];
    [contentLab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(energyTitleLab);
        make.bottom.mas_offset(-18);
    }];
    
    _dchgTodayLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kColorRGBA(63, 68, 67,0.6)];
    _dchgTodayLab.text = @"--kWh";
    [energyView addSubview:_dchgTodayLab];
    [_dchgTodayLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentLab1.mas_right);
        make.centerY.equalTo(contentLab1);
    }];
    
    UIImageView *arrowImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow"]];
    [energyView addSubview:arrowImg];
    [arrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.right.mas_offset(-15);
        make.centerY.mas_offset(0);
    }];
    
    QMUIButton *arrowBtn = [[QMUIButton alloc]init];
    [energyView addSubview:arrowBtn];
    [arrowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    [[arrowBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.chartAction){
            weakSelf.chartAction();
        }
    }];
}

//家储示意图 (能量,馈网,电池,光伏,负载)
-(UIView*)indicatorViewWithTitle:(NSString*)title unit:(NSString*)unitStr Alignment:(NSTextAlignment)alignment;
{
    UIView *bgView = kView;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = kColorRGBA(255, 255, 255, 0.5);
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.greaterThanOrEqualTo(@(80));
    }];
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.textAlignment = alignment;
    titleLab.numberOfLines = 0;
    titleLab.text = title;
    titleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:1];
    titleLab.font = [UIFont systemFontOfSize:10];
    [bgView addSubview:titleLab];
    
    QMUILabel *contentLab = [[QMUILabel alloc]init];
    contentLab.textAlignment = alignment;
    contentLab.text = [NSString stringWithFormat:@"-- %@",unitStr];
    contentLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:1];
    contentLab.font = [UIFont systemFontOfSize:14];
    contentLab.tag = 100;
    [bgView addSubview:contentLab];
    
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(titleLab);
        make.top.mas_offset(8);
        make.height.offset(20);
    }];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(8);
        make.right.mas_offset(-8);
        make.top.equalTo(contentLab.mas_bottom);
        make.bottom.mas_offset(-8);
       
    }];
    
    return bgView;
}

-(void)setProductType:(NSString *)productType
{
    kWeakSelf;
    if(![_productType isEqualToString:productType]){
        _productType = productType;
        if([_productType isEqualToString:@"ME_DC"]||[_productType isEqualToString:@"ME_AC"]){
            
            [_topoImg setImage:[UIImage imageNamed:@"topo_micro"]];
            [_topoImg mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(25);
                make.right.mas_offset(-25);
                make.top.mas_offset(15);
                make.height.equalTo(weakSelf.topoImg.mas_width).multipliedBy(561.0/648.0);
            }];
            //馈网
            [_feed_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
            
                make.centerX.equalTo(weakSelf.topoImg.mas_left).offset(kScaleW(20));
                make.bottom.equalTo(weakSelf.topoImg).offset(-kScaleW(35));
            }];
            
            //电池
            [_battery_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(weakSelf.topoImg).offset(kScaleW(-55));
                make.bottom.equalTo(weakSelf.topoImg).offset(-kScaleW(5));
            }];
            
            //能量
//            [_power_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.right.equalTo(weakSelf.battery_indicator);
//                make.width.offset(120);
//                make.bottom.equalTo(weakSelf.feed_indicator.mas_top).offset(-kScaleW(20));
//            }];
            
            //负载
            [_load_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(weakSelf.topoImg.mas_centerX).offset(kScaleW(25));
               
                make.bottom.equalTo(weakSelf.topoImg).offset(kScaleW(5));
            }];
            
            //光伏
            [_solar_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(weakSelf.topoImg.mas_centerX).offset(kScaleW(-45));
        
                make.top.equalTo(weakSelf.topoImg).offset(kScaleW(-10));
            }];
        }else{//默认HE_BAT
            [_topoImg setImage:[UIImage imageNamed:@"local_topo_hebat"]];
            [_topoImg mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_offset(-5);
                make.left.mas_offset(50);
                make.top.mas_offset(25);
                make.height.equalTo(weakSelf.topoImg.mas_width).multipliedBy(561.0/648.0);
            }];
            
            //馈网
            [_feed_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(weakSelf.topoImg.mas_left).offset(kScaleW(30));
                make.top.equalTo(weakSelf.topoImg).offset(-10);
            }];
            
            //电池
            [_battery_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(weakSelf.topoImg.mas_left).offset(kScaleW(75));
                make.top.equalTo(weakSelf.topoImg.mas_bottom).offset(kScaleW(5));
            }];
            
            //能量
            [_power_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(weakSelf.battery_indicator);
                make.bottom.equalTo(weakSelf.feed_indicator.mas_top).offset(-kScaleW(20));
            }];
            _power_indicator.hidden = YES;
            
            //负载
            [_load_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.offset(kScaleW(100));
                make.bottom.equalTo(weakSelf.topoImg).offset(kScaleW(18));
            }];
            
            //光伏
            [_solar_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.offset(kScaleW(20));
                make.bottom.equalTo(weakSelf.topoImg.mas_top).offset(kScaleW(28));
            }];
        }
    }
}

@end
